<template>
	<view class="container">
		<uni-nav-bar title="贵烟查询" :fixed="true" :statusBar="true" :border="false" rightText="管理" leftText="新增"
			@clickLeft="goEdit('-1')" @clickRight="goManager">
		</uni-nav-bar>
		<view class="topContainer">
			<view
				style="padding: 5rpx	align-items: center;		align-content: center; display: flex;justify-content: center;">
				<button type="primary" size="mini" @click="scanClick">扫码 </button>
			</view>
			<view style="  flex: 1; padding: 5rpx;">
				<uni-easyinput type="text" v-model="searchValue" placeholder="输入条码或者点击扫描" />
				<!-- <input class="uni-inputt text" v-model="searchValue" placeholder="输入条码或者点击扫描" style="flex: 1; " e /> -->
			</view>
			<view
				style="padding: 5rpx	align-items: center;		align-content: center; display: flex;justify-content: center;">
				<button type="primary" size="mini" @click="searchHandler">查询 </button>
			</view>
		</view>
		<view style="flex:1; background-color: white;overflow-y:hidden;">
			<scroll-view :scroll-y="true" style="height: 100%; padding-bottom: 20px;" @scrolltolower="onReachBottom1()"
				@scrolltoupper="onPullDownRefresh1()">
				<uni-card v-for="(item ,index) of dataList" :key="index" :title="item.serial_no+'.'+item.name"
					:sub-title="item.money" :extra="item.address" @click="goEdit(item.id)">
					<!-- 	<text class="uni-body">{{item.date}}：{{item.source}}</text> -->
					<view>零售价:
						<text class="mark_title">￥{{ item.price_sales }}</text>
						<text class="mark_title"
							style="font-size: 20rpx;color: dimgray;text-decoration: line-through;">￥{{ item.price_order }}</text>
					</view>
					<view>包码:
						<text class="">{{ item.code_bag }}</text>
					</view>
					<view>条码:
						<text class="">{{ item.code_carton }}</text>
					</view>
				</uni-card>
				<view style="height: 10px;"></view>
			</scroll-view>
		</view>
	</view>
</template>
<script>
	import modal from "@/utils/modal.js"
	import dbOper from '@/module/accountSqlOper'
	export default {
		data() {
			return {
				statusBarHeight: 0,
				dbIsOpen: false,
				searchValue: "",
				dataList: [],
				queryParams: {
					pageNum: 1,
					pageSize: 10,
					searchValue: ""
				}
			}
		},
		onShow() {
			this.searchHandler()
		},
		methods: {
			scanClick() {
				uni.scanCode({
					scanType: ['barCode', 'qrCode', 'pdf417'],
				}).then(data => {
					var [err, res] = data;
					this.searchValue = res.result
					this.searchHandler()
				})
			},
			goManager() { //跳转到管理页面
				uni.navigateTo({
					url: '/pages/manager/manager'
				});
			},
			selectUserInfo(queryParams) {
				let total = 0;
				return dbOper.getTotal(queryParams.searchValue)
					.then(totalRes => {
						total = totalRes
						if (total > 0) {
							return dbOper.getList(queryParams.searchValue, queryParams.pageSize, queryParams.pageNum)
						} else {
							return Promise.resolve([])
						}
					})
					.then(res => {
						return Promise.resolve({
							rows: res,
							total: total
						})
					})
			},
			pageList() { //查询一页数据
				modal.loading()
				// console.log("pageList", this.queryParams);
				return this.selectUserInfo(this.queryParams)
					.then(res => {
						for (let item of res.rows) {
							this.dataList.push(item)
						}
						// console.log("数据记录数", res.total);
						this.total = res.total
						modal.closeLoading()
					})
					.catch(err => {
						modal.closeLoading()
						modal.msg(err)
					})
			},
			searchHandler() { //点击查询事件
				this.queryParams.pageNum = 1
				this.queryParams.searchValue = this.searchValue
				this.dataList = []
				this.total = 0
				return this.pageList()
			},
			onReachBottom1() { //上拉加载
				console.log("上拉加载更多", this.total, this.dataList.length);
				if (this.total <= this.dataList.length) {
					modal.msg("没有更多了")
					return
				}
				this.queryParams.pageNum += 1
				this.pageList()
			},
			onPullDownRefresh1() { //下拉刷新
				this.searchHandler()
					.then(res => {
						uni.stopPullDownRefresh()
					})
					.catch(err => {})
			},
			goEdit(id) { //跳转到编辑页面
				uni.navigateTo({
					url: '/pages/edit/edit?id=' + id,
					fail(err) {
						console.log("e", err);
					}
				});
			}
		}
	}
</script>

<style>
	.topContainer {
		display: flex;
		flex-direction: row;
		justify-content: space-around;
		width: calc(100vw - 20rpx);
		border-bottom: 1rpx solid gray;
		padding: 0 10rpx;
		align-items: center;
		align-content: center;
	}

	.container {
		overflow: hidden;
		width: 100%;
		height: calc(100vh);
		display: flex;
		flex-direction: column;
	}

	.allItemView {
		margin-top: 5px;
		padding: 2px;
		background-color: #f8f8f8;
	}

	.mark_title {
		color: #DD524D;
	}
</style>